﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
using DevExpress.Web.ASPxEditors;
using Utility1;

namespace Thv.admin.Pages
{
    public partial class quanlySanpham : System.Web.UI.UserControl
    {
        protected string template_path;
        protected void Page_Load(object sender, EventArgs e)
        {
            template_path = WebUtils.SystemUrl();
            if (!IsPostBack)
            {
                Bincate();
                bindata();
                grvTrangthai();
            }
        }

        void Bincate()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.Categories
                       where k.flag == true
                       select k;
            cbLoai.DataSource = list;
            cbLoai.ValueField = "ID";
            cbLoai.TextField = "CateName";
            cbLoai.DataBind();
            cbLoai.SelectedIndex = 0;
        } 
        void bindata()
        {
            DataClasses1DataContext dx = new DataClasses1DataContext();
            var list = from k in dx.Product_tbs
                       where k.iCateID == int.Parse(cbLoai.Value.ToString())
                       select k;
            grvData.DataSource = list;
            grvData.DataBind();
        }
        protected void cbPanel_Callback(object sender, DevExpress.Web.ASPxClasses.CallbackEventArgsBase e)
        {
            switch (e.Parameter)
            {
                case "ChangeLoai":
                    bindata();
                    grvTrangthai();
                    break;
                case "Xoa":
                    DataClasses1DataContext dx = new DataClasses1DataContext();
                    int id = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "ID");
                    Product_tb p = new Product_tb();
                    p = dx.Product_tbs.Single(f => f.ID==id);
                    dx.Product_tbs.DeleteOnSubmit(p);
                    dx.SubmitChanges();
                    bindata();
                    break;
                case "Duyet":
                    DataClasses1DataContext dx1 = new DataClasses1DataContext();
                    int id1 = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "ID");
                    var list = from k in dx1.Product_tbs
                               where k.ID == id1
                               select k;
                    bool kich=false;
                    foreach (Product_tb pr in list)
                        kich = pr.iflag;
                    if (kich == true)
                        kich = false;
                    else
                        kich = true;
                    Product_tb p1 = new Product_tb();
                    p = dx1.Product_tbs.Single(f => f.ID==id1);
                    p.iflag = kich;
                    dx1.SubmitChanges();
                    bindata();
                    grvTrangthai();
                    break;
                case "Xem":
                    int id2 = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "Id");
                    popup.ContentUrl = template_path + "/admin/Pages/editsanpham.aspx?Id=" + id2;
                    popup.ShowLoadingPanel = true;
                    popup.ShowOnPageLoad = true;
                    popup.ShowShadow = true;

                    break;
            }
        }

        protected void img_Init(object sender, EventArgs e)
        {
            ASPxImage img = (ASPxImage)sender;
            GridViewDataItemTemplateContainer templateContainer = (GridViewDataItemTemplateContainer)img.NamingContainer;
            string id = templateContainer.KeyValue.ToString();
            img.Attributes.Add("onmouseover", "OnImageMouseOver(this);");

            string link = template_path + "/admin/Pages/editsanpham.aspx?Id=" + id;
            //img.Attributes.Add("onmouseout", "OnImageMouseOut(this);");
            img.Attributes.Add("value", link);
        }
        void grvTrangthai()
        {
            GridViewDataColumn column1 = this.grvData.Columns["Action"] as GridViewDataColumn;
            for (int j = 0; j < grvData.VisibleRowCount; j++)
            {

                HiddenField hdfId = (HiddenField)this.grvData.FindRowCellTemplateControl(j, column1, "hdfFlag");
                ASPxImage ASPxImage2 = (ASPxImage)this.grvData.FindRowCellTemplateControl(j, column1, "ASPxImage2");
                if (hdfId.Value == "false")
                {
                    ASPxImage2.ImageUrl = template_path + "/admin/images/approve.png";
            
                }
            }
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            int id = (int)grvData.GetRowValues(grvData.FocusedRowIndex, "ID");
            string link = template_path + "/admin/index.aspx?page=edit-product&&id="+id;
            Response.Redirect(link);
        }

    }
}